package com.ningmengs789.live.ui.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.ningmengs789.live.R;
import com.ningmengs789.live.live.live.common.widget.gift.AbsDialogFragment;
import com.ningmengs789.live.model.SXModel;
import com.ningmengs789.live.ui.CommonCallback;
import com.ningmengs789.live.utils.ASXUtil;
import com.ningmengs789.live.utils.CommonUtils;

public class SXDialogFragment extends AbsDialogFragment implements View.OnClickListener {

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_sx;
    }

    @Override
    protected int getDialogStyle() {
        return R.style.BottomDialog;
    }

    @Override
    protected boolean canCancel() {
        return true;
    }

    @Override
    protected void setWindowAttributes(Window window) {
        window.setWindowAnimations(R.style.BottomDialog_Animation);
        WindowManager.LayoutParams params = window.getAttributes();
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        params.height = CommonUtils.dip2px(mContext, 320);
        params.gravity = Gravity.BOTTOM;
        window.setAttributes(params);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
//        Bundle bundle = getArguments();
//        if (bundle == null) {
//            return;
//        }

        LXs = new TextView[]{
                (TextView) findViewById(R.id.sx_l_1),
                (TextView) findViewById(R.id.sx_l_2),
                (TextView) findViewById(R.id.sx_l_3),
                (TextView) findViewById(R.id.sx_l_4),
                (TextView) findViewById(R.id.sx_l_5),
        };

        for (int i = 0; i < LXs.length; i++) {
            final int pos = i;
            LXs[i].setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    for (int j = 0; j < LXs.length; j++) {
                        if (pos == j){
                            sxModel.setSex(pos + 1);
                            LXs[j].setBackgroundResource(ASXUtil.shuxingBG[pos]);
                            LXs[j].setTextColor(Color.parseColor("#ffffff"));
                        }else {
                            LXs[j].setBackgroundResource(R.drawable.d_sx_bg_2);
                            LXs[j].setTextColor(Color.parseColor("#9d9d9d"));
                        }
                    }
                }
            });
        }

        SXs = new TextView[]{
                (TextView) findViewById(R.id.sx_s_1),
                (TextView) findViewById(R.id.sx_s_2),
                (TextView) findViewById(R.id.sx_s_3)
        };

        for (int i = 0; i < SXs.length; i++) {
            final int pos = i;
            SXs[i].setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    for (int j = 0; j < SXs.length; j++) {
                        if (pos == j){
                            sxModel.setShuxing(pos);
                            SXs[j].setBackgroundResource(R.drawable.d_sx_bg_3);
                            SXs[j].setTextColor(Color.parseColor("#ffffff"));
                        }else {
                            SXs[j].setBackgroundResource(R.drawable.d_sx_bg_2);
                            SXs[j].setTextColor(Color.parseColor("#9d9d9d"));
                        }
                    }
                }
            });
        }

        sxJuli = (TextView) findViewById(R.id.sx_juli);
        sxJuli.setOnClickListener(this);

        findViewById(R.id.sx_btn).setOnClickListener(this);
    }

    private TextView[] LXs;
    private TextView[] SXs;
    private TextView sxJuli;

    private SXModel sxModel;

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.sx_juli) {
            if (sxModel.getJuli() == 0){
                sxModel.setJuli(1);
                sxJuli.setBackgroundResource(R.drawable.d_sx_bg_3);
                sxJuli.setTextColor(Color.parseColor("#ffffff"));
            }else {
                sxModel.setJuli(0);
                sxJuli.setBackgroundResource(R.drawable.d_sx_bg_2);
                sxJuli.setTextColor(Color.parseColor("#9d9d9d"));
            }
        } else if (v.getId() == R.id.sx_btn){
            mCallback.callback(sxModel);
            dismiss();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    private CommonCallback<SXModel> mCallback;

    public void setCallback(CommonCallback<SXModel> callback) {
        sxModel = new SXModel(-1, -1, 0);
        mCallback = callback;
    }
}
